package network;

import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.List;

/**
 * @author Erik
 * Common functions for the DataHandleListeners-Interface
 */

public class Listener {
	protected List<DataHandleListener> listener;

	public void addListener(DataHandleListener newlistener) {
		listener.add(newlistener);
	}

	public void notifyListener(SocketChannel socket, String dataPackage, int packageID) {
		synchronized (listener) {
			for (Iterator<DataHandleListener> iterator = listener.iterator(); iterator
					.hasNext();) {
				iterator.next().notifyDataToHandle( socket, dataPackage,
						packageID);
			}
		}
	}

	public void notifyPlayerDisconnected(SocketChannel socket) {
		synchronized (listener) {
			for (Iterator<DataHandleListener> iterator = listener.iterator(); iterator
					.hasNext();) {
				iterator.next().notifyPlayerDisconnected(socket);
			}
		}
	}
	
	public void notifyPlayerConnected(SocketChannel socket, int playerID) {
		synchronized (listener) {
			for (Iterator<DataHandleListener> iterator = listener.iterator(); iterator
					.hasNext();) {
				iterator.next().notifyPlayerConnected(socket, playerID);
			}
		}
	}
}
